using System;
using System.Collections.Generic;
using System.Text;
using DTO;
using System.Data;
using System.Data.SqlClient;

namespace DAO
{
	/// <summary> 
	///Author: nnamthach@gmail.com 
	/// <summary>
    public class IMPORT_SCHEMADataAccess : Connection
    {
		#region Local Variable
        private string _strSPInsertName = "dbo.[procIMPORT_SCHEMA_add]";
        private string _strSPUpdateName = "dbo.[procIMPORT_SCHEMA_update]";
        private string _strSPDeleteName = "dbo.[procIMPORT_SCHEMA_delete]";
        private string _strSPGetName = "dbo.[procIMPORT_SCHEMA_get]";
        private string _strSPGetAllName = "dbo.[procIMPORT_SCHEMA_getall]";
		private string _strSPGetPages = "dbo.[procIMPORT_SCHEMA_getpaged]";
		private string _strSPIsExist = "dbo.[procIMPORT_SCHEMA_isexist]";
        private string _strTableName = "[IMPORT_SCHEMA]";
		private string _strSPGetTransferOutName = "dbo.[procIMPORT_SCHEMA_gettransferout]";
		#endregion Local Variable
		
		#region Method
        public IMPORT_SCHEMAInfo Get(
        String CONN_ID,
        String SCHEMA_ID,
		ref string sErr)
        {
			IMPORT_SCHEMAInfo objEntr = new IMPORT_SCHEMAInfo();
			InitConnect();
			InitSPCommand(_strSPGetName);              
            AddParameter(IMPORT_SCHEMAInfo.Field.CONN_ID.ToString(), CONN_ID);
            AddParameter(IMPORT_SCHEMAInfo.Field.SCHEMA_ID.ToString(), SCHEMA_ID);
            
            DataTable list = new DataTable();
            try
            {
                list = executeSelect();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            finally {closeConnection();}  
            
            if (list.Rows.Count > 0)
                objEntr = (IMPORT_SCHEMAInfo)GetDataFromDataRow(list, 0);
            //if (dr != null) list = CBO.FillCollection(dr, ref list);
            if (sErr != "") ErrorLog.SetLog(sErr);
            return objEntr;
        }

        protected override object GetDataFromDataRow(DataTable dt, int i)
        {
            IMPORT_SCHEMAInfo result = new IMPORT_SCHEMAInfo();
            result.CONN_ID = (dt.Rows[i][IMPORT_SCHEMAInfo.Field.CONN_ID.ToString()] == DBNull.Value ? "" : Convert.ToString(dt.Rows[i][IMPORT_SCHEMAInfo.Field.CONN_ID.ToString()]));
            result.SCHEMA_ID = (dt.Rows[i][IMPORT_SCHEMAInfo.Field.SCHEMA_ID.ToString()] == DBNull.Value ? "" : Convert.ToString(dt.Rows[i][IMPORT_SCHEMAInfo.Field.SCHEMA_ID.ToString()]));
            result.LOOK_UP = (dt.Rows[i][IMPORT_SCHEMAInfo.Field.LOOK_UP.ToString()] == DBNull.Value ? "" : Convert.ToString(dt.Rows[i][IMPORT_SCHEMAInfo.Field.LOOK_UP.ToString()]));
            result.DESCRIPTN = (dt.Rows[i][IMPORT_SCHEMAInfo.Field.DESCRIPTN.ToString()] == DBNull.Value ? "" : Convert.ToString(dt.Rows[i][IMPORT_SCHEMAInfo.Field.DESCRIPTN.ToString()]));
            result.FIELD_TEXT = (dt.Rows[i][IMPORT_SCHEMAInfo.Field.FIELD_TEXT.ToString()] == DBNull.Value ? "" : Convert.ToString(dt.Rows[i][IMPORT_SCHEMAInfo.Field.FIELD_TEXT.ToString()]));
            result.DB = (dt.Rows[i][IMPORT_SCHEMAInfo.Field.DB.ToString()] == DBNull.Value ? "" : Convert.ToString(dt.Rows[i][IMPORT_SCHEMAInfo.Field.DB.ToString()]));
            result.DAG = (dt.Rows[i][IMPORT_SCHEMAInfo.Field.DAG.ToString()] == DBNull.Value ? "" : Convert.ToString(dt.Rows[i][IMPORT_SCHEMAInfo.Field.DAG.ToString()]));
            result.SCHEMA_STATUS = (dt.Rows[i][IMPORT_SCHEMAInfo.Field.SCHEMA_STATUS.ToString()] == DBNull.Value ? "" : Convert.ToString(dt.Rows[i][IMPORT_SCHEMAInfo.Field.SCHEMA_STATUS.ToString()]));
            result.UPDATED = (dt.Rows[i][IMPORT_SCHEMAInfo.Field.UPDATED.ToString()] == DBNull.Value ? 0 : Convert.ToInt32(dt.Rows[i][IMPORT_SCHEMAInfo.Field.UPDATED.ToString()]));
            result.ENTER_BY = (dt.Rows[i][IMPORT_SCHEMAInfo.Field.ENTER_BY.ToString()] == DBNull.Value ? "" : Convert.ToString(dt.Rows[i][IMPORT_SCHEMAInfo.Field.ENTER_BY.ToString()]));
            result.DEFAULT_CONN = (dt.Rows[i][IMPORT_SCHEMAInfo.Field.DEFAULT_CONN.ToString()] == DBNull.Value ? "" : Convert.ToString(dt.Rows[i][IMPORT_SCHEMAInfo.Field.DEFAULT_CONN.ToString()]));
           
            return result;
        }

        public DataTable GetAll(string con,
        ref string sErr)
        {
            InitConnect();
            InitSPCommand(_strSPGetAllName);
            AddParameter("CONN_ID", con);
            DataTable list = new DataTable();
            try
            {
                list = executeSelect();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            finally {closeConnection();}


            if (sErr != "") ErrorLog.SetLog(sErr);
            return list;
        }
		/// <summary>
        /// Return 1: Table is exist Identity Field
        /// Return 0: Table is not exist Identity Field
        /// Return -1: Erro
        /// </summary>
        /// <param name="tableName"></param>
        public Int32 Add(IMPORT_SCHEMAInfo objEntr, ref string sErr)
        {
            int ret = -1;
            openConnection();
            InitSPCommand(_strSPInsertName);
            AddParameter(IMPORT_SCHEMAInfo.Field.CONN_ID.ToString(), objEntr.CONN_ID);
            AddParameter(IMPORT_SCHEMAInfo.Field.SCHEMA_ID.ToString(), objEntr.SCHEMA_ID);
            AddParameter(IMPORT_SCHEMAInfo.Field.LOOK_UP.ToString(), objEntr.LOOK_UP);
            AddParameter(IMPORT_SCHEMAInfo.Field.DESCRIPTN.ToString(), objEntr.DESCRIPTN);
            AddParameter(IMPORT_SCHEMAInfo.Field.FIELD_TEXT.ToString(), objEntr.FIELD_TEXT);
            AddParameter(IMPORT_SCHEMAInfo.Field.DB.ToString(), objEntr.DB);
            AddParameter(IMPORT_SCHEMAInfo.Field.DAG.ToString(), objEntr.DAG);
            AddParameter(IMPORT_SCHEMAInfo.Field.SCHEMA_STATUS.ToString(), objEntr.SCHEMA_STATUS);
            AddParameter(IMPORT_SCHEMAInfo.Field.UPDATED.ToString(), objEntr.UPDATED);
            AddParameter(IMPORT_SCHEMAInfo.Field.ENTER_BY.ToString(), objEntr.ENTER_BY);
            AddParameter(IMPORT_SCHEMAInfo.Field.DEFAULT_CONN.ToString(), objEntr.DEFAULT_CONN);
          
            try
            {
                //command.ExecuteNonQuery();
                object tmp = executeScalar();
                if(tmp != null && tmp != DBNull.Value)
					ret = Convert.ToInt32(tmp);
				else 
					ret=0;
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            finally {closeConnection();}
            if (sErr != "") ErrorLog.SetLog(sErr);
			
            return ret;
        }

        public string Update(IMPORT_SCHEMAInfo objEntr)
        {
            openConnection();
            InitSPCommand(_strSPUpdateName);
            AddParameter(IMPORT_SCHEMAInfo.Field.CONN_ID.ToString(), objEntr.CONN_ID);
            AddParameter(IMPORT_SCHEMAInfo.Field.SCHEMA_ID.ToString(), objEntr.SCHEMA_ID);
            AddParameter(IMPORT_SCHEMAInfo.Field.LOOK_UP.ToString(), objEntr.LOOK_UP);
            AddParameter(IMPORT_SCHEMAInfo.Field.DESCRIPTN.ToString(), objEntr.DESCRIPTN);
            AddParameter(IMPORT_SCHEMAInfo.Field.FIELD_TEXT.ToString(), objEntr.FIELD_TEXT);
            AddParameter(IMPORT_SCHEMAInfo.Field.DB.ToString(), objEntr.DB);
            AddParameter(IMPORT_SCHEMAInfo.Field.DAG.ToString(), objEntr.DAG);
            AddParameter(IMPORT_SCHEMAInfo.Field.SCHEMA_STATUS.ToString(), objEntr.SCHEMA_STATUS);
            AddParameter(IMPORT_SCHEMAInfo.Field.UPDATED.ToString(), objEntr.UPDATED);
            AddParameter(IMPORT_SCHEMAInfo.Field.ENTER_BY.ToString(), objEntr.ENTER_BY);
            AddParameter(IMPORT_SCHEMAInfo.Field.DEFAULT_CONN.ToString(), objEntr.DEFAULT_CONN);
               
            string sErr = "";
            try
            {
                executeNonQuery();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            finally {closeConnection();}
            if (sErr != "") ErrorLog.SetLog(sErr);
            return sErr;
        }

        public string Delete(
        String CONN_ID,
        String SCHEMA_ID
		)
        {
            openConnection();
            InitSPCommand(_strSPDeleteName);
            AddParameter(IMPORT_SCHEMAInfo.Field.CONN_ID.ToString(), CONN_ID);
            AddParameter(IMPORT_SCHEMAInfo.Field.SCHEMA_ID.ToString(), SCHEMA_ID);
              
            string sErr = "";
            try
            {
                executeNonQuery();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            finally {closeConnection();}
            if (sErr != "") ErrorLog.SetLog(sErr);
            return sErr;
        }   
		
		
        
        public Boolean IsExist(
        String CONN_ID,
        String SCHEMA_ID
		)
        {
            InitConnect();
            InitSPCommand(_strSPIsExist);
            AddParameter(IMPORT_SCHEMAInfo.Field.CONN_ID.ToString(), CONN_ID);
            AddParameter(IMPORT_SCHEMAInfo.Field.SCHEMA_ID.ToString(), SCHEMA_ID);
              
            string sErr = "";
            DataTable list = new DataTable();
            try
            {
                list = executeSelect();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            finally {closeConnection();}
            if (sErr != "") ErrorLog.SetLog(sErr);
            if(list.Rows.Count==1)
				return true;
            return false;
        }
		
		private string CreateWhereClause(IMPORT_SCHEMAInfo obj)
        {
            String result = "";

            return result;
        }
        
        public DataTable Search(string columnName, string columnValue, string condition, string tableName, ref string sErr)
        {
            string query = "select * from " + _strTableName + " where " + columnName + " " + condition + " " + columnValue;
            DataTable list = new DataTable();
            openConnection();
            try
            {
                list = executeSelectQuery(query);
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            finally {closeConnection();}
            //if (dr != null) list = CBO.FillCollection(dr, ref list);
            //    if (sErr != "") ErrorLog.SetLog(sErr);
            return list;
        }
		public DataTable GetTransferOut(string dtb, object from, object to, ref string sErr)
        {
            InitConnect();
            InitSPCommand(_strSPGetTransferOutName);
			AddParameter("DB", dtb);
			AddParameter("FROM", from);
			AddParameter("TO", to);
            DataTable list = new DataTable();
            try
            {
                list = executeSelect();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            finally {closeConnection();}


            if (sErr != "") ErrorLog.SetLog(sErr);
            return list;
        }
		#endregion Method
     
    }
}
